package com.zs4sdk.activity;

import java.util.ArrayList;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

import com.zs4sdk.Zs4GameBase;
import com.zs4sdk.Zs4GameMenu;
import com.zs4sdk.common.Extra;
import com.zs4sdk.entity.Zs4GameData;
import com.zs4sdk.entity.MenuItemJSONResultEntity;
import com.common.library.util.ActivityHelper;
import com.common.library.util.LogUtil;

/**
 * function: 云项菜单
 *
 * @ author: aXiBa
 */
public class Zs4GameGridMenuActivity extends Zs4GameBaseActivity {

    public static final String TAG = Zs4GameGridMenuActivity.class.getSimpleName();


    ImageButton mCloseView;

    Button updateAccountButton;


//
    String pageName = "";
    BroadcastReceiver menuUpdateCompleteReceiver = null;

    ArrayList<MenuItemJSONResultEntity> data;

    String curLang = "";

    private Button mPayButton, mAccountButton, mFeedbackButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(ActivityHelper.getLayoutResId("zs4gamelayout_view_menugrid"));


        init();
    }


    protected void init() {


        initReferences();

        pageName = Zs4GameBase.getPackageName();
    }

    // 注册菜单完成广播接收
    private void registerMenuUpdateReceiver() {

        menuUpdateCompleteReceiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                LogUtil.i(TAG, "菜单更新完成接收...");

                Zs4GameGridMenuActivity.this.unregisterReceiver(menuUpdateCompleteReceiver);
                menuUpdateCompleteReceiver = null;

                // 显示菜单
                initMenuViews();
            }
        };
        IntentFilter filter = new IntentFilter(pageName.concat(Extra.ACTIONKEY.UPDATEMENUCOMPLETE_POSTFIX_MENU));
        registerReceiver(menuUpdateCompleteReceiver, filter);
    }


    @Override
    protected void onDestroy() {

        if (null != menuUpdateCompleteReceiver) {
            Zs4GameGridMenuActivity.this.unregisterReceiver(menuUpdateCompleteReceiver);
            menuUpdateCompleteReceiver = null;
        }
        super.onDestroy();
    }

    // 初始化菜单
    private void initMenuViews() {


    }


    private void initReferences() {

        mCloseView = (ImageButton) findViewById(ActivityHelper.getIdResId("zs4gameviewid_mCloseView_view_menugrid"));
//        mPayButton = (Button) findViewById(ActivityHelper.getIdResId("zs4gameviewid_menu_btn_pay"));
//        mAccountButton = (TextView) findViewById(ActivityHelper.getIdResId("zs4gameviewid_menu_btn_account"));
//        mFeedbackButton = (TextView) findViewById(ActivityHelper.getIdResId("zs4gameviewid_menu_btn_kefu"));
        updateAccountButton = (Button) findViewById(ActivityHelper.getIdResId("zs4gameviewid_menu_btn_goto_update_account"));

        updateAccountButton.setText(ActivityHelper.getStringResId("zs4gamestring_menugrid_ensure_update"));

        setViewClickEvent(this,
                ActivityHelper.getIdResId("zs4gameviewid_menu_btn_account"),
//                ActivityHelper.getIdResId("zs4gameviewid_menu_btn_pay"),
                ActivityHelper.getIdResId("zs4gameviewid_menu_btn_kefu"),
                ActivityHelper.getIdResId("zs4gameviewid_mCloseView_view_menugrid"),
                ActivityHelper.getIdResId("zs4gameviewid_menu_btn_goto_update_account"));
    }

    @Override
    public void onClick(View v) {

        super.onClick(v);
        // 用户中心
        if (v.getId() == ActivityHelper.getIdResId("zs4gameviewid_menu_btn_account")) {
            Zs4GameMenu.enterAccountCenter(Zs4GameGridMenuActivity.this, flag, pageName);
            finish();
        }
//        //支付
//        else if (v.getId() == ActivityHelper.getIdResId("zs4gameviewid_menu_btn_pay")) {
//            Zs4GameMenu.enterVoucherCenter(this, flag, pageName, "","");
//            finish();
//        }
        //客服
        else if (v.getId() == ActivityHelper.getIdResId("zs4gameviewid_menu_btn_kefu")) {
            Zs4GameMenu.showWebView(this,
                    "在线客服",
                    Extra.URL.SDKGAME_FEEDBACK,
                    flag,
                    "3");
            finish();
        }
        //关闭
        else if (v.getId() == ActivityHelper.getIdResId("zs4gameviewid_mCloseView_view_menugrid")) {
            backToGame();
        }
        //绑定
        else if (v.getId() == ActivityHelper.getIdResId("zs4gameviewid_menu_btn_goto_update_account")) {
            if(Zs4GameData.getInstance().isGuest()){
                goBindSafePhone();
                finish();
            }else {
                ActivityHelper.showToast(ActivityHelper.getStringResId("zs4gamestring_menu_showtoast_tip"));
            }
        }

    }
    // 到绑定安全手机
    private void goBindSafePhone() {

        Intent intent = new Intent(this, Zs4GameBindSafePhoneActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt(Extra.INTENTKEY.SCREEN_ORIENTATION, this.mScreenOrientation);
        bundle.putString(Extra.INTENTKEY.FLAG, flag);
        intent.putExtras(bundle);
        startActivity(intent);

    }


    @Override
    public void onBackPressed() {
        backToGame();
    }

}
